package com.aw.manual.ejemplo014;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.MethodOverride;
import org.springframework.beans.factory.support.MethodOverrides;
import org.springframework.beans.factory.support.ReplaceOverride;
import org.springframework.context.annotation.ScannedGenericBeanDefinition;
import org.springframework.core.type.MethodMetadata;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * User: gmc
 * Date: 04/06/2010
 */
@Component
public class MethodReplacerDecoratorFactoryPostProcessor implements BeanFactoryPostProcessor {
    private static final String REPLACED_BY_CLASS_NAME = ReplacedBy.class.getName();

    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        String[] beanDefinitionNames = configurableListableBeanFactory.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition(beanDefinitionName);
            if (beanDefinition instanceof ScannedGenericBeanDefinition) {
                ScannedGenericBeanDefinition scannedBeanDefinition = (ScannedGenericBeanDefinition) beanDefinition;
                Set<MethodMetadata> methodsMetadata = scannedBeanDefinition.getMetadata().getAnnotatedMethods(REPLACED_BY_CLASS_NAME);
                if (methodsMetadata.size() > 0) {
                    configureMethodsToBeOverridden(scannedBeanDefinition, methodsMetadata);
                }
            }
        }
    }

    private void configureMethodsToBeOverridden(ScannedGenericBeanDefinition scannedBeanDefinition, Set<MethodMetadata> methodsMetadata) {
        MethodOverrides mo = new MethodOverrides();
        for (MethodMetadata methodMetadata : methodsMetadata) {
            String methodName = methodMetadata.getMethodName();
            Map<String, Object> attributes = methodMetadata.getAnnotationAttributes(REPLACED_BY_CLASS_NAME);
            MethodOverride methodOverride = new ReplaceOverride(methodName, (String) attributes.get("value"));
            mo.addOverride(methodOverride);
        }
        scannedBeanDefinition.setMethodOverrides(mo);
    }

}
